package com.gr.staffpm;

import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.gr.menuw.horizontal.dropdown.multilevel.MultiLevelMenu;
import com.gr.menuw.horizontal.dropdown.multilevel.MultiLevelSubMenu;
import com.gr.menuw.menu.item.LinkMenuItem;
import com.gr.staffpm.datatypes.Task;
import com.gr.staffpm.datatypes.User;
import com.gr.staffpm.notes.NotesPanel;
import com.gr.staffpm.pages.behaviour.SubmitOnEnterKeyBehaviour;
import com.gr.staffpm.pages.behaviour.TextFieldWatermarkBehaviour;
import com.gr.staffpm.pages.dashboard.Dashboard;
import com.gr.staffpm.pages.login.Login;
import com.gr.staffpm.pages.logout.Logout;
import com.gr.staffpm.pages.profile.ViewProfile;
import com.gr.staffpm.pages.search.QuickSearch;
import com.gr.staffpm.pages.tasks.ViewTask;
import com.gr.staffpm.search.Indexer;
import com.gr.staffpm.security.service.UserService;
import com.gr.staffpm.tasks.service.TaskService;


public class HeaderPanel extends Panel {

    @SpringBean(name = "taskService")
    private TaskService taskService;
    private static final long serialVersionUID = 1L;
    private TextField<String> searchHeaderField;

    public HeaderPanel(UserService userService, String id) {
        super(id);

        Indexer.getInstance().createIndex(taskService);

        User user = userService.getCurrentUser();
        if (user == null) {
            setResponsePage(Login.class);
        } else {

            add(new BookmarkablePageLink<Void>("homeLink", Dashboard.class));

            MultiLevelMenu menu = new MultiLevelMenu("profile-menu");
            MultiLevelSubMenu submenu = new MultiLevelSubMenu("1", user.getFirstName() + " " + user.getLastName(), menu);

            submenu.addItem(new LinkMenuItem("2", "Profile", ViewProfile.class));
            submenu.addItem(new LinkMenuItem("3", "Logout", Logout.class));

            add(menu);

            NotesPanel notes = new NotesPanel("notes");
            add(notes);

            Form<String> searchHeaderForm = new Form<String>("searchHeaderForm") {

                private static final long serialVersionUID = 1L;

                @Override
                protected void onSubmit() {
                    searchTasks();
                }
            };
            add(searchHeaderForm);
            searchHeaderField = new TextField<String>("searchHeaderField", new Model<String>());
            searchHeaderField.add(new TextFieldWatermarkBehaviour("Quick Task Seach"));
            searchHeaderField.add(new SubmitOnEnterKeyBehaviour(searchHeaderForm));
            searchHeaderForm.add(searchHeaderField);

        }

    }

    protected void searchTasks() {
        List<Task> results = taskService.findTask(searchHeaderField.getValue());
        if (results.size() > 1)
            setResponsePage(new QuickSearch(results));
        else if (results.size() == 1)
            setResponsePage(ViewTask.class, new PageParameters("id=" + results.get(0).getTaskId()));
    }

}
